package com.ruyuan.eshop.common.domain.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ruyuan.eshop.common.enums.*;
import com.ruyuan.eshop.framework.domain.entity.BaseEntity;
import com.ruyuan.eshop.common.domain.vo.PriceDetailVO;
import com.ruyuan.eshop.common.domain.dto.OrderDTO;
import com.ruyuan.eshop.common.domain.vo.ConsigneeVO;
import com.ruyuan.eshop.common.domain.vo.OrderParam;
import com.ruyuan.eshop.common.domain.vo.OrderSkuVO;
import com.ruyuan.eshop.framework.utils.JsonUtil;
import lombok.Data;

import java.util.List;


/**
 * 订单表实体
 *
 * @author pwd
 * @version 1.0
 */
@TableName("es_order")
@Data
public class OrderDO extends BaseEntity {

    /**
     * 交易编号
     */
    @TableField(value = "trade_sn")
    private String tradeSn;

    /**
     * 订单编号
     */
    @TableField(value = "sn")
    private String sn;

    /**
     * 店铺ID
     */
    @TableField(value = "seller_id")
    private Long sellerId;

    /**
     * 店铺名称
     */
    @TableField(value = "seller_name")
    private String sellerName;

    /**
     * 会员ID
     */
    @TableField(value = "member_id")
    private Long memberId;

    /**
     * 买家账号
     */
    @TableField(value = "member_name")
    private String memberName;

    /**
     * 订单状态
     */
    @TableField(value = "order_status")
    private String orderStatus;

    /**
     * 付款状态
     */
    @TableField(value = "pay_status")
    private String payStatus;

    /**
     * 货运状态
     */
    @TableField(value = "ship_status")
    private String shipStatus;

    /**
     * 配送方式ID
     */
    @TableField(value = "shipping_id")
    private Integer shippingId;

    /**
     * 评论是否完成
     */
    @TableField(value = "comment_status")
    private String commentStatus;

    /**
     * 配送方式
     */
    @TableField(value = "shipping_type")
    private String shippingType;

    /**
     * 支付方式id
     */
    @TableField(value = "payment_method_id")
    private Long paymentMethodId;

    /**
     * 支付插件id
     */
    @TableField(value = "payment_plugin_id")
    private String paymentPluginId;

    /**
     * 支付方式名称
     */
    @TableField(value = "payment_method_name")
    private String paymentMethodName;

    /**
     * 支付方式类型
     */
    @TableField(value = "payment_type")
    private String paymentType;

    /**
     * 支付时间
     */
    @TableField(value = "payment_time")
    private Long paymentTime;

    /**
     * 已支付金额
     */
    @TableField(value = "pay_money")
    private Double payMoney;

    /**
     * 收货人姓名
     */
    @TableField(value = "ship_name")
    private String shipName;

    /**
     * 收货地址
     */
    @TableField(value = "ship_addr")
    private String shipAddr;

    /**
     * 收货人邮编
     */
    @TableField(value = "ship_zip")
    private String shipZip;

    /**
     * 收货人手机
     */
    @TableField(value = "ship_mobile")
    private String shipMobile;

    /**
     * 收货人电话
     */
    @TableField(value = "ship_tel")
    private String shipTel;

    /**
     * 收货时间
     */
    @TableField(value = "receive_time")
    private String receiveTime;

    /**
     * 配送地区-省份ID
     */
    @TableField(value = "ship_province_id")
    private Long shipProvinceId;

    /**
     * 配送地区-城市ID
     */
    @TableField(value = "ship_city_id")
    private Long shipCityId;

    /**
     * 配送地区-区(县)ID
     */
    @TableField(value = "ship_county_id")
    private Long shipCountyId;

    /**
     * 配送街道id
     */
    @TableField(value = "ship_town_id")
    private Long shipTownId;

    /**
     * 配送地区-省份
     */
    @TableField(value = "ship_province")
    private String shipProvince;

    /**
     * 配送地区-城市
     */
    @TableField(value = "ship_city")
    private String shipCity;

    /**
     * 配送地区-区(县)
     */
    @TableField(value = "ship_county")
    private String shipCounty;

    /**
     * 配送街道
     */
    @TableField(value = "ship_town")
    private String shipTown;

    /**
     * 订单总额
     */
    @TableField(value = "order_price")
    private Double orderPrice;

    /**
     * 商品总额
     */
    @TableField(value = "goods_price")
    private Double goodsPrice;

    /**
     * 配送费用
     */
    @TableField(value = "shipping_price")
    private Double shippingPrice;

    /**
     * 优惠金额
     */
    @TableField(value = "discount_price")
    private Double discountPrice;

    /**
     * 是否被删除
     */
    @TableField(value = "disabled")
    private Integer disabled;

    /**
     * 订单商品总重量
     */
    @TableField(value = "weight")
    private Double weight;

    /**
     * 商品数量
     */
    @TableField(value = "goods_num")
    private Integer goodsNum;

    /**
     * 订单备注
     */
    @TableField(value = "remark")
    private String remark;

    /**
     * 订单取消原因
     */
    @TableField(value = "cancel_reason")
    private String cancelReason;

    /**
     * 签收人
     */
    @TableField(value = "the_sign")
    private String theSign;

    /**
     * 转换 List<OrderSkuVO>
     */
    @TableField(value = "items_json")
    private String itemsJson;

    @TableField(value = "warehouse_id")
    private Integer warehouseId;

    @TableField(value = "need_pay_money")
    private Double needPayMoney;

    @TableField(value = "ship_no")
    private String shipNo;

    /**
     * 收货地址ID
     */
    @TableField(value = "address_id")
    private Long addressId;

    /**
     * 管理员备注
     */
    @TableField(value = "admin_remark")
    private Integer adminRemark;

    /**
     * 物流公司ID
     */
    @TableField(value = "logi_id")
    private Long logiId;

    /**
     * 物流公司名称
     */
    @TableField(value = "logi_name")
    private String logiName;

    /**
     * 完成时间
     */
    @TableField(value = "complete_time")
    private Long completeTime;

    /**
     * 签收时间
     */
    @TableField(value = "signing_time")
    private Long signingTime;

    /**
     * 送货时间
     */
    @TableField(value = "ship_time")
    private Long shipTime;

    /**
     * 支付方式返回的交易号
     */
    @TableField(value = "pay_order_no")
    private String payOrderNo;

    /**
     * 售后状态
     */
    @TableField(value = "service_status")
    private String serviceStatus;

    /**
     * 结算状态
     */
    @TableField(value = "bill_status")
    private Integer billStatus;

    /**
     * 结算单号
     */
    @TableField(value = "bill_sn")
    private String billSn;

    /**
     * 订单来源
     */
    @TableField(value = "client_type")
    private String clientType;

    @TableField(value = "need_receipt")
    private Integer needReceipt;

    /**
     * 因增加拼团业务新增订单类型字段
     */
    @TableField(value = "order_type")
    private String orderType;


    /**
     * 订单的扩展数据
     * 为了增加订单的扩展性，个性化的业务可以将个性化数据（如拼团所差人数）存在此字段
     */
    @TableField(value = "order_data")
    private String orderData;

    /**
     * 预存款抵扣金额
     */
    @TableField(value = "balance")
    private Double balance;

    public Double getOrderPrice() {
        if (orderPrice == null) {
            orderPrice = 0.0d;
        }
        return orderPrice;
    }

    public Double getGoodsPrice() {
        if (goodsPrice == null) {
            goodsPrice = 0.0d;
        }
        return goodsPrice;
    }

    public Double getShippingPrice() {
        if (shippingPrice == null) {
            shippingPrice = 0.0d;
        }
        return shippingPrice;
    }

    public Double getDiscountPrice() {
        if (discountPrice == null) {
            discountPrice = 0.0d;
        }
        return discountPrice;
    }

    public Double getBalance() {
        if (balance == null) {
            balance = 0.0d;
        }
        return balance;
    }

    public OrderDO() {

    }

    /**
     * 带初始化参数的构造器
     *
     * @param orderDTO
     */
    public OrderDO(OrderDTO orderDTO) {

        this.receiveTime = orderDTO.getReceiveTime();
        this.sn = orderDTO.getSn();
        this.shipTime = orderDTO.getShipTime();
        this.paymentType = orderDTO.getPaymentType();

        // 收货人
        ConsigneeVO consignee = orderDTO.getConsignee();
        if(consignee!=null){
            shipName = consignee.getName();
            shipAddr = consignee.getAddress();

            this.addressId = consignee.getConsigneeId();
            this.shipMobile = consignee.getMobile();
            this.shipTel = consignee.getTelephone();

            this.shipProvince = consignee.getProvince();
            this.shipCity = consignee.getCity();
            this.shipCounty = consignee.getCounty();
            this.shipTown = consignee.getTown();

            this.shipProvinceId = consignee.getProvinceId();
            this.shipCityId = consignee.getCityId();
            this.shipCountyId = consignee.getCountyId();
            this.shipTownId = consignee.getTownId();
        }


        // 价格
        PriceDetailVO priceDetail = orderDTO.getPrice();
        this.orderPrice = orderDTO.getOrderPrice();
        this.goodsPrice = priceDetail.getGoodsPrice();
        this.shippingPrice = priceDetail.getFreightPrice();
        this.discountPrice = priceDetail.getDiscountPrice();
        this.needPayMoney = orderDTO.getNeedPayMoney();
        this.weight = orderDTO.getWeight();
        this.shippingId = orderDTO.getShippingId();
        this.balance = 0D;

        // 卖家
        this.sellerId = orderDTO.getSellerId();
        this.sellerName = orderDTO.getSellerName();

        // 买家
        this.memberId = orderDTO.getMemberId();
        this.memberName = orderDTO.getMemberName();

        // 初始化状态
        this.shipStatus = ShipStatusEnum.SHIP_NO.value();
        this.orderStatus = OrderStatusEnum.NEW.value();
        this.payStatus = PayStatusEnum.PAY_NO.value();
        this.commentStatus = CommentStatusEnum.UNFINISHED.value();
        this.serviceStatus = OrderServiceStatusEnum.NOT_APPLY.value();
        this.disabled = 0;

        List<OrderSkuVO> orderSkuVOList = orderDTO.getOrderSkuList();
        for (OrderSkuVO orderSkuVO : orderSkuVOList) {
            orderSkuVO.setActualPayTotal(orderSkuVO.getSubtotal());
            orderSkuVO.setComplainStatus(ComplainSkuStatusEnum.NO_APPLY.name());
        }

        // 产品列表
        this.itemsJson = JsonUtil.objectToJson(orderSkuVOList);

        // 备注
        this.remark = orderDTO.getRemark();

        //发票
        this.needReceipt = orderDTO.getNeedReceipt();

        //订单来源
        this.clientType = orderDTO.getClientType();
        this.orderType = orderDTO.getOrderType();

    }

    /**
     * 带初始化参数的构造器
     * @param orderParam
     */
    public OrderDO(OrderParam orderParam){

    }

}
